package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmFinanceInfo;
import com.nuoniu.sibanyun.service.crm.ICrmFinanceInfoService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 财务信息 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/crm/finance")
@Api(value="财务信息(往来单位 及 开票信息)",tags={"财务信息"})
public class CrmFinanceInfoController extends BaseController {

    @Autowired
    private ICrmFinanceInfoService financeInfoService;

    /**
     * 获取财务信息列表
     * @author 小啊刚
     * @date 2020/6/24
     */
    @GetMapping("page")
    @ApiOperation(value = "分页",tags = "APP_财务信息")
    public HttpResult<Page<CrmFinanceInfo>> getFinanceInfoList(Page page, CrmFinanceInfo financeInfo) {
        Page<CrmFinanceInfo> financeInfoPage = financeInfoService.getFinanceInfoList(page, financeInfo);
        return HttpResult.ok(financeInfoPage);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("detail")
    @ApiOperation(value = "详情",tags = "APP_财务信息")
    public HttpResult<CrmFinanceInfo> detail(Long id) {
        CrmFinanceInfo one = financeInfoService.getById(id);
        return HttpResult.ok(one);
    }
    /**
     * 保存财务信息
     * @author 小啊刚
     * @date 2020/6/24
     */
    @PostMapping("save")
    @ApiOperation(value = "保存",tags = "APP_财务信息")
    public HttpResult saveFinanceInfo(@RequestBody CrmFinanceInfo financeInfo) {
        UserInfo userInfo = UserThreadLocal.get();
        LocalDateTime now = LocalDateTime.now();
        if(financeInfo.getId() == null) {
            //校验 财务信息是否已经存在
            CrmFinanceInfo oldCrmFinanceInfo = financeInfoService.getByCrmId(financeInfo.getCrmId());
            if(null != oldCrmFinanceInfo){
                financeInfo.setId(oldCrmFinanceInfo.getId());
            }
            financeInfo.setCreateUserId(userInfo.getUserId());
            financeInfo.setCreateUserName(userInfo.getRealName());
            financeInfo.setCompanyId(userInfo.getCompanyId());
        }else {
            financeInfo.setUpdateUserId(userInfo.getUserId());
            financeInfo.setUpdateDate(now);
            financeInfo.setUpdateUserName(userInfo.getRealName());
        }
        financeInfoService.saveOrUpdate(financeInfo);
        return HttpResult.ok();
    }

    /**
     * 删除财务信息
     * @author 小啊刚
     * @date 2020/6/24
     */
    @PostMapping("delete")
    public HttpResult deleteFinanceInfo(@RequestBody List<Long> ids) {
        if(null != ids) {
            financeInfoService.removeByIds(ids);
        }
        return HttpResult.ok();
    }

    /**
     * 停用财务信息
     */
    @PostMapping(value = "disable")
    @ApiOperation(value = "停用",tags = "APP_财务信息")
    public HttpResult disableSelected(List<Long> ids) {
        financeInfoService.disableSelected(ids);
        return HttpResult.ok("停用财务信息成功");
    }

    /**
     * 启用财务信息
     */
    @RequestMapping(value = "enable")
    @ApiOperation(value = "启用",tags = "APP_财务信息")
    public HttpResult enableSelected(List<Long> ids) {
        financeInfoService.enableSelected(ids);
        return HttpResult.ok("启用财务信息成功");
    }

}
